// MouseStatus.cpp
//
#include "MouseStatus.h"

#include <QGraphicsView>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include <QtDebug>
//#include <windows.h>

using namespace IDS::IDEAS::OpenGLView::Fwk;

// ------------------------------------------------------------------------------------------------
MouseStatus::MouseStatus()
{
	reset();
}

// -----------------------------------------------------------------------------------------------
MouseStatus::Button MouseStatus::eventToButton( QGraphicsSceneMouseEvent *e )
{
	int btn = BUTTON_NONE;

	Qt::MouseButton qtbtn           = e->button();
	Qt::KeyboardModifiers modifiers = e->modifiers();

	if (qtbtn & Qt::LeftButton)				btn |= BUTTON_LEFT;
	if (qtbtn & Qt::RightButton)			btn |= BUTTON_RIGHT;
	if (qtbtn & Qt::MidButton)				btn |= BUTTON_MIDDLE;

	if (modifiers & Qt::ShiftModifier)		btn |= KEY_SHIFT;
	if (modifiers & Qt::ControlModifier)	btn |= KEY_CTRL;
	if (modifiers & Qt::AltModifier)		btn |= KEY_ALT;

	return Button (btn);
}

// -----------------------------------------------------------------------------------------------
MouseStatus::Button MouseStatus::eventToButton( QMouseEvent *e )
{
	int btn = BUTTON_NONE;

	Qt::MouseButton qtbtn           = e->button();
	Qt::KeyboardModifiers modifiers = e->modifiers();

	if (qtbtn & Qt::LeftButton)				btn |= BUTTON_LEFT;
	if (qtbtn & Qt::RightButton)			btn |= BUTTON_RIGHT;
	if (qtbtn & Qt::MidButton)				btn |= BUTTON_MIDDLE;

	if (modifiers & Qt::ShiftModifier)		btn |= KEY_SHIFT;
	if (modifiers & Qt::ControlModifier)	btn |= KEY_CTRL;
	if (modifiers & Qt::AltModifier)		btn |= KEY_ALT;

	return Button (btn);
}
	
// -----------------------------------------------------------------------------------------------
void MouseStatus::reset()
{
	xPos = xPrev = -1;
	yPos = yPrev = -1;
	pressedButton  = BUTTON_NONE;
	releasedButton = BUTTON_NONE;
	moving = false;	
}


// -----------------------------------------------------------------------------------------------
void MouseStatus::setView(QGraphicsView *gv)
{
	view = gv;	
}

// -----------------------------------------------------------------------------------------------
void MouseStatus::set(QGraphicsSceneMouseEvent *e, bool updateButtons, bool invertY )
{
	xPrev = xPos;
	yPrev = yPos;

	QPoint p = view->mapFromGlobal( e->screenPos() );

	xPos = p.x();
	yPos = invertY ? view->scene()->height()-p.y()-1 : p.y();

	moving = (e->type() == e->GraphicsSceneMouseMove);
	//int dist = (xPos-xPrev)*(xPos-xPrev) + (yPos-yPrev)*(yPos-yPrev);
	//if(e->type() == e->GraphicsSceneMouseMove && dist > MOVING_THRESHOLD*MOVING_THRESHOLD)
	//	moving = true;

	releasedButton = BUTTON_NONE;
	if(updateButtons)
	{
		MouseStatus::Button btn = eventToButton(e);
			
		if      (e->type() == QEvent::GraphicsSceneMousePress)
		{
			pressedButton |=   btn;
		}
		else if (e->type() == QEvent::GraphicsSceneMouseRelease)
		{
			pressedButton &= ~(btn);		
			releasedButton |= btn;
		}
	}

#if 0
	QString ss = QString("[%1|%2] %3\n").arg(pressedButton).arg(releasedButton).arg( e->type() );
		
	OutputDebugStringA(ss.toLocal8Bit().data());
#endif
	e->accept();
}

// -----------------------------------------------------------------------------------------------
void MouseStatus::set(QMouseEvent *e, bool updateButtons, bool invertY )
{
	xPrev = xPos;
	yPrev = yPos;

	QPoint p = e->pos();

	xPos = p.x();
	yPos = invertY ? view->scene()->height()-p.y()-1 : p.y();

	moving = (e->type() == e->MouseMove);
	//int dist = (xPos-xPrev)*(xPos-xPrev) + (yPos-yPrev)*(yPos-yPrev);
	//if(e->type() == e->GraphicsSceneMouseMove && dist > MOVING_THRESHOLD*MOVING_THRESHOLD)
	//	moving = true;

	releasedButton = BUTTON_NONE;
	if(updateButtons)
	{
		MouseStatus::Button btn = eventToButton(e);
			
		if      (e->type() == QEvent::MouseButtonPress)
		{
			pressedButton |=   btn;
		}
		else if (e->type() == QEvent::MouseButtonRelease)
		{
			pressedButton &= ~(btn);		
			releasedButton |= btn;
		}
	}

#if 0
	QString ss = QString("[%1|%2] %3\n").arg(pressedButton).arg(releasedButton).arg( e->type() );
		
	OutputDebugStringA(ss.toLocal8Bit().data());
#endif
	e->accept();
}